# -*- coding: UTF-8 -*-
from django.shortcuts import render
from django.shortcuts import HttpResponse,render_to_response

#Django处理http请求的函数的参数至少得有一个参数request
#包含请求的信息
def login(request):
    #如果是GET请求
    #print request.method    #打印客户请求的方式
    #如果是POST，检验用户输入
   if request.method == "POST":
        input_email = request.POST['email']
        input_pwd = request.POST['pwd']
        from django.shortcuts import redirect

        #获取用户输入，并将取转成MD5加密后的格式
        import hashlib
        hash = hashlib.md5()
        hash.update(input_pwd)
        input_pwd_md5 = hash.hexdigest()

        #获取UserInfo表中所有的数据
        from app01 import models
        user_info_list = models.UserInfo.objects.all()
        for i in user_info_list:
            E = i.email
            P = i.pwd
            if input_email ==  E and input_pwd_md5 == P:
                #这个位置设置登陆成功后跳转的地方
                return redirect("/son/")
            else:
                print 'no'
                #下面是一个BUG，打开就不能登陆成功了，可以试试redirect
                #return render(request,'login.html',{'status':'用户名或密码错误'})
   return  render(request,'login.html')

def index(request):
    #数据库取数据
    #数据库和HTML渲染
    from app01 import models
    if request.method == "POST":
        #添加数据,用户与密码
        input_em = request.POST['em']
        input_pw = request.POST['pw']
        #密码MD5验证
        import hashlib
        hash = hashlib.md5()
        hash.update(input_pw)
        MD5 = hash.hexdigest()
        #注册，写入MD5加密过的验证，并检验用户是否已经存在
        XX = True
        ALL_user =  models.UserInfo.objects.all()
        for i in  ALL_user:
            user = i.email
            if user == input_em:
                XX = False
                break
        print XX,'cishidezhuantai'
        if XX == True:
            models.UserInfo.objects.create(email=input_em,pwd=MD5)

        #models.UserInfo.objects.filter(email=input_em,pwd=input_pw).delete()
        #models.UserInfo.objects.filter(email=input_em).update(pwd='888')

    #获取UserInfo表中所有的数据
    user_info_list = models.UserInfo.objects.all()
    #user_info_list列表，列表的元素 #一行，email,pwd
    return render(request,'index.html',{'user_info_list':user_info_list})
def son(request):
    return render(request,'son1.html')

def home(request):
    #return HttpResponse('ok')
    #找到home.html
    #读取html.返回给用户
    #跨站请求伪造
    dic = {'name':'ZHANGTING','age':18,'user_list':['zt1','zt2','zt3']}
    return render(request,'home.html',dic)

#重置密码
def chongzhimima(request):
    from app01 import models
    if request.method == "POST":
        #添加数据,用户与密码
        input_em = request.POST['em']
        input_pw = request.POST['pw']
        #密码MD5验证
        import hashlib
        hash = hashlib.md5()
        hash.update(input_pw)
        MD5 = hash.hexdigest()
        #重置密码，写入MD5加密过的验证，并检验用户是否已经存在
        XX = True
        ALL_user =  models.UserInfo.objects.all()
        for i in  ALL_user:
            user = i.email
            if user == input_em:
                XX = False
                break
        print XX,'cishidezhuantai'
        #找到了，则重置密码
        if XX == False:
            models.UserInfo.objects.filter(email=input_em).update(pwd=MD5)
    #获取UserInfo表中所有的数据
    user_info_list = models.UserInfo.objects.all()
    #user_info_list列表，列表的元素 #一行，email,pwd
    return render(request,'chongzhimima.html',{'user_info_list':user_info_list})

#创建用户
def chuangjianyongfu(request):
    from app01 import models
    if request.method == "POST":
        #添加数据,用户与密码
        input_em = request.POST['em']
        input_pw = request.POST['pw']
        #密码MD5验证
        import hashlib
        hash = hashlib.md5()
        hash.update(input_pw)
        MD5 = hash.hexdigest()
        #注册，写入MD5加密过的验证，并检验用户是否已经存在
        XX = True
        ALL_user =  models.UserInfo.objects.all()
        for i in  ALL_user:
            user = i.email
            if user == input_em:
                XX = False
                break
        print XX,'cishidezhuantai'
        #True表示没有找到，则创建用户
        if XX == True:
            models.UserInfo.objects.create(email=input_em,pwd=MD5)
    #获取UserInfo表中所有的数据
    user_info_list = models.UserInfo.objects.all()
    #user_info_list列表，列表的元素 #一行，email,pwd
    return render(request,'chuangjianyongfu.html',{'user_info_list':user_info_list})

#修改用户
def xiugaiyongfu(request):
    from app01 import models
    if request.method == "POST":
        #添加数据,用户与密码
        input_em = request.POST['em']
        input_pw = request.POST['pw']
        XX = True
        ALL_user =  models.UserInfo.objects.all()
        for i in  ALL_user:
            user = i.email
            if user == input_em:
                XX = False
                break
        print XX,'cishidezhuantai'
        #找到了，则修改用户
        if XX == False:
            models.UserInfo.objects.filter(email=input_em).update(email=input_pw)
    #获取UserInfo表中所有的数据
    user_info_list = models.UserInfo.objects.all()
    #user_info_list列表，列表的元素 #一行，email,pwd
    return render(request,'xiugaiyongfu.html',{'user_info_list':user_info_list})

#删除用户
def shuanchuyongfu(request):
    #数据库取数据
    #数据库和HTML渲染
    from app01 import models
    if request.method == "POST":
        #添加数据,用户与密码
        input_em = request.POST['em']
        #input_pw = request.POST['pw']
        XX = True
        ALL_user =  models.UserInfo.objects.all()
        for i in  ALL_user:
            user = i.email
            if user == input_em:
                XX = False
                break
        print XX,'cishidezhuantai_mima'
        if XX == False:
            models.UserInfo.objects.filter(email=input_em).delete()

    #获取UserInfo表中所有的数据
    user_info_list = models.UserInfo.objects.all()
    #user_info_list列表，列表的元素 #一行，email,pwd
    return render(request,'shuanchuyongfu.html',{'user_info_list':user_info_list})

#查看用户
def chakanyongfu(request):
    #数据库取数据
    #数据库和HTML渲染
    from app01 import models
    #获取UserInfo表中所有的数据
    user_info_list = models.UserInfo.objects.all()
    #user_info_list列表，列表的元素 #一行，email,pwd
    return render(request,'chakanyongfu.html',{'user_info_list':user_info_list})

#批量创建
def piliangchuangjian(request):
    from app01 import models
    if request.method == "POST":
        #添加数据,用户与密码
        #input_em = request.POST['em']
        #input_pw = request.POST['pw']
        input_pl = request.POST['pl']

        #注册，写入MD5加密过的验证，并检验用户是否已经存在
        #append转换成列表并split分隔出帐号与密码
        a = []
        a.append(input_pl)
        #print a,'1'
        for i in a:
            x =i.split(';')
            #print x,'2'
            for i1 in x:
                mm = i1.split(':')[-1]
                yf = i1.split(':')[0]
                #密码MD5验证
                import hashlib
                hash = hashlib.md5()
                hash.update(mm)
                MD5 = hash.hexdigest()

                #检查用户是否是存在，不存在则创建
                XX = True
                ALL_user =  models.UserInfo.objects.all()
                for i in  ALL_user:
                    user = i.email
                    if user == yf:
                        XX = False
                        break
                print XX,'cishidezhuantai'
                #True表示没有找到，则创建用户
                if XX == True:
                    models.UserInfo.objects.create(email=yf,pwd=MD5)
    #获取UserInfo表中所有的数据
    user_info_list = models.UserInfo.objects.all()
    #user_info_list列表，列表的元素 #一行，email,pwd
    return render(request,'piliangchuangjian.html',{'user_info_list':user_info_list})
